package com.jinke.uang.ui;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.jinke.uang.R;
import com.jinke.uang.common.RouterMap;
import com.jinke.uang.common.UserInfo;
import com.jinke.uang.utils.StringUtils;
import com.jinke.uang.utils.ToastUtils;
import com.jinke.uang.utils.toolutil.MathUtil;

import androidx.appcompat.app.AppCompatActivity;

/**
 * @author lixiaonan
 * 功能描述: 计算按钮
 * 时 间： 2019-05-25 13:29
 */
public class MainCaActivity extends AppCompatActivity {

    private Button submit;
    private EditText id,etlixi,tvTime;
    private TextView result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mainca);
        id = (EditText) findViewById(R.id.id);
        etlixi = (EditText) findViewById(R.id.etlixi);
        tvTime = (EditText) findViewById(R.id.etTime);
        result = (TextView) findViewById(R.id.result);
        submit = (Button) findViewById(R.id.submit);
        submit.setOnClickListener(paramView -> {
            if (StringUtils.isBlank(UserInfo.getToken())) {
                //Facebook登录
                ARouter.getInstance().build(RouterMap.LOGIN_METNOD_PAGE).navigation();
                finish();
            } else {
                //计算金额
                String money=id.getText().toString().trim();
                String rate=etlixi.getText().toString().trim();
                String time=tvTime.getText().toString().trim();
                if(StringUtils.isBlank(money)||StringUtils.isBlank(rate)
                        ||StringUtils.isBlank(time)){
                    ToastUtils.showShort(MainCaActivity.this,"Silakan masukkan informasi");
                    return;
                }
                result.setText(MathUtil.calculateMoneyNo(money,rate,time));
            }
        });
        result.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                String bb = result.getText().toString().trim();
                if (null == bb || bb.equals("")) {
                    return true;
                }
                ClipboardManager clipboardManager =
                        (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                clipboardManager.setPrimaryClip(ClipData.newPlainText(null, bb));
                if (clipboardManager.hasPrimaryClip()) {
                    clipboardManager.getPrimaryClip().getItemAt(0).getText();
                }
                return true;
            }
        });
    }
}
